<?php



if(isset($_POST['id'])) {
	$rsharing=new Sharing($_POST['id']);
	$rsharing->load();
	
		if(($_POST['vendor']=="vendor")&&(isset($rsharing['vendor']['id']))) {
			$vendor=$rsharing->getVendor();
			$brand=Brand::getFromName($rsharing['name']); 
		}
		elseif(($_POST['vendor']=="vendor")&&(!isset($rsharing['vendor']['id']))) {
			$vendor=new Vendor();
			$brand=new Brand();
			$brand['nom']=trim($_POST['name']);
			$brand['slug']=trim($_POST['slug']);
			$brand->save();
			$vendor['nom']=trim($_POST['name']);
			$vendor['slug']=trim($_POST['slug']);
			$vendor->save();

			$db=Database::getInstance();
			$sql="INSERT INTO sharing_isvendor(id_sharing,id_vendor) VALUES ('".$rsharing['id']."','".$vendor['id']."')";
			$result = $db->query($sql);
		}
		elseif(($_POST['vendor']=="")&&(isset($rsharing['vendor']['id']))) {
			$vendor=$rsharing->getVendor();
			$brand=Brand::getFromName($rsharing['name']);
			$brand->delete();
			$vendor->delete();
			$db=Database::getInstance();
				$sql="DELETE FROM brands_vendors_link WHERE vendor_id='".$vendor['id']."'";
				$result=$db->query($sql);
				$sql="DELETE FROM sharing_isvendor WHERE id_vendor='".$vendor['id']."'";
				$result=$db->query($sql);
		}
	
	$rsharing['name']=trim($_POST['name']);
	$rsharing['slug']=trim($_POST['slug']);
	$rsharing['margin']=trim($_POST['margin']);
	$rsharing['email']=trim($_POST['email']);
	$rsharing['type']=$_POST['type'];
	$rsharing['box_pub']=$_POST['box'];
		if(($_POST['password']==$_POST['password2'])&&($_POST['password']!='')) {
				$rsharing['password']=md5(trim($_POST['password']));
				if(isset($vendor)) {$vendor['password']=md5(trim($_POST['password'])); $vendor->save();}
			}
	$rsharing->save();	

}
else{
	$rsharing=new Sharing();
	$rsharing['name']=trim($_POST['name']);
	$rsharing['slug']=trim($_POST['slug']);
	$rsharing['margin']=trim($_POST['margin']);
	$rsharing['email']=trim($_POST['email']);
		if(($_POST['password']==$_POST['password2'])&&($_POST['password']!='')) {
			$rsharing['password']=md5(trim($_POST['password']));
		}
	$rsharing['type']=$_POST['type'];
	$rsharing['box_pub']=$_POST['box'];
	$rsharing->save();

		if($_POST['vendor']=="vendor") {
			$vendor=new Vendor();
			$brand=new Brand();
			$brand['nom']=trim($_POST['name']);
			$brand['slug']=trim($_POST['slug']);
			$brand->save();
			$vendor['nom']=trim($_POST['name']);
			$vendor['slug']=trim($_POST['slug']);
			$vendor['email']=trim($_POST['email']);
			if(($_POST['password']==$_POST['password2'])&&($_POST['password']!='')) {
			$vendor['password']=md5(trim($_POST['password']));
			}
			$vendor->save();

			$db=Database::getInstance();
			$sql="INSERT INTO sharing_isvendor(id_sharing,id_vendor) VALUES ('".$rsharing['id']."','".$vendor['id']."')";
			$result = $db->query($sql);
		}

}

$nav=Navigation::getInstance();
if ((!empty($_FILES['userfile']['name'][0]))&&(isset($rsharing['id']))){
								$extension = strtolower(strrchr($_FILES['userfile']['name'][0], '.')); 
								$nom=$nav->getRewrite(str_replace($extension, '', $_FILES['userfile']['name'][0]));
								$name=$nom.$extension;
								$fichier = basename($name);
				
								$dossier = UP_IMG_PATH.'sharing/'.$rsharing['id'].'/';
				if($name!="") {
								$rsharing['logo']=$name;
								$rsharing->save();	
							  }
								if(!is_dir($dossier)) {
								mkdir ($dossier, 0777); 
								}
								if(!isset($erreur)) //S'il n'y a pas d'erreur, on upload
									{//On formate le nom du fichier ici...
										move_uploaded_file($_FILES['userfile']['tmp_name'][0], $dossier . $fichier);
									}
								else{ echo $erreur;}						
				}

if ((!empty($_FILES['userfile']['name'][1]))&&(isset($rsharing['id']))){
								$extension = strtolower(strrchr($_FILES['userfile']['name'][1], '.')); 
								$nom=$nav->getRewrite(str_replace($extension, '', $_FILES['userfile']['name'][1]));
								$name=$nom.$extension;
								$fichier = basename($name);
				
								$dossier = UP_IMG_PATH.'sharing/'.$rsharing['sharing']['id'].'/';
				if($name!="") {
								$rsharing['img_ban']=$name;
								$rsharing->save();
							  }
								if(!is_dir($dossier)) {
								mkdir ($dossier, 0777); 
								}

								if(!isset($erreur)) //S'il n'y a pas d'erreur, on upload
									{//On formate le nom du fichier ici...
										move_uploaded_file($_FILES['userfile']['tmp_name'][0], $dossier . $fichier);
									}
								else{ echo $erreur;}						
				}






?>